<?php
/**
 * 文件名：topic.mod.php
 * 版本号：1.0
 * 最后修改时间：2009年9月28日 14时10分42秒
 * 作者：狐狸<foxis@qq.com>
 * 功能描述: 微博话题模块
 */

if(!defined('IN_JISHIGOU'))
{
	exit('invalid request');
}

class ModuleObject extends MasterObject
{
	var $ShowConfig;

	var $CacheConfig;

	var $TopicLogic;

	var $ID = '';


	function ModuleObject($config)
	{
		$this->MasterObject($config);

		$this->ID = (int) ($this->Post['id'] ? $this->Post['id'] : $this->Get['id']);

		Load::logic('topic');
		$this->TopicLogic = new TopicLogic($this);

		$this->CacheConfig = ConfigHandler::get('cache');

		$this->ShowConfig = ConfigHandler::get('show');
        
		$this->Execute();

	}


	function Execute()
	{
		//DatabaseHandler::$noCache =false;
		ob_start();
		if('fans' == $this->Code) {
			$this->Fans();
		} elseif ('follow' == $this->Code) {
			$this->Follow();
		} elseif ('top' == $this->Code) {
			DatabaseHandler::$noCache =false;
			$this->Top();
		} elseif ('group' == $this->Code) {
			$this->ViewGroup();
		} elseif (in_array($this->Code,array('new','forward','reply','newreply'))) {
			DatabaseHandler::$noCache =false;
			$this->Hot();
		} elseif ('home' == $this->Code) {
			$this->_guestIndex();
		} elseif ('view' == $this->Code) {
			$this->View();
		} elseif (is_numeric($this->Code)) {
			$this->ID = (int) $this->Code;
			$this->View();
		} elseif ('cms' == $this->Code) {
			$this->Cms();
		}else {
			$this->Main();
		}
		$body=ob_get_clean();

		$this->ShowBody($body);
	}

	function Main()
	{
		extract($this->Get);
		extract($this->Post);
		if('topic'==$this->Get['mod'] && count($this->Get)<2) {


			if (MEMBER_ID > 0)
			{
				$this->Code = 'myhome';
			}
			else
			{
				$this->_guestIndex();
				return ;
			}
		}


		$title = '';
		if(isset($uid)) $uid = (int) $uid;
		$per_page_num = 20;			$topic_uids = $topic_ids = $order_list = $where_list = $params = array();
		$where = $order = $limit = "";
		$order_list['dateline'] = "`dateline` desc";
		$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&code={$this->Code}" : "");

		if('myat' == $this->Code) {

			$title = '@提到我的';
			$topic_selected = 'myat';

			$uid = MEMBER_ID;
			$member = $this->_member($uid);
			if (!$member) {
				$this->_guestIndex();
				return false;
			}
			if($member['uid']!=MEMBER_ID) {
				$this->Messager("您无权查看该页面",null);
			}

			if ($member['at_new']) {
				$sql = "update `".TABLE_PREFIX."members` set `at_new`=0 where `uid`='{$member['uid']}'";
				$this->DatabaseHandler->Query($sql);

				$this->MemberHandler->MemberFields['at_new'] = 0;
			}

			$sql = "select * from `".TABLE_PREFIX."topic_mention` where `uid`='{$uid}'";
			$query = $this->DatabaseHandler->Query($sql);
			$topic_ids[0] = 0;
			while ($row = $query->GetRow())
			{
				$topic_ids[$row['tid']] = $row['tid'];
			}

		} else {
	
			$member = $this->_member();
		
			if(!$member) {
				$this->_guestIndex();
				return false;
			}
		}

		$params['uid'] = $uid = $member['uid'];
		if($uid != MEMBER_ID || $_GET[mod_original]) {
			$follow_list = $this->_followList($params['uid']);
		} else {
			$recommend_user_list = $this->_Recommenduser(1);

			$my_favorite_tags = $this->_myFavoriteTags(12);

		}

		$is_personal = ($uid == MEMBER_ID);
		$params['is_personal'] = $is_personal;

		$start = max(0, (int) $start);
		$limit = "limit {$start},{$per_page_num}";
		$next = $start + $per_page_num;

		$params['code'] = $this->Code;
		if (!in_array($params['code'],array('myblog','mycomment','tocomment','myhome','myat','myfavorite','favoritemy','tag','groupview'))) {
			$params['code'] = 'myblog';		}

//			if (($show_topic_num = (int) $this->ShowConfig['topic'][$params['code']]) > 0) {
//				$per_page_num = $show_topic_num;
//			}
   

			if ('myhome'==$params['code']) {

				$topic_selected = 'myhome';

					
				$where = " and `type` != 'reply' ";

				if($this->Get['type'])
				{
					$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&code={$this->Code}&type={$this->Get['type']}" : "");

					if('pic' == $this->Get['type']){
						$where = " and `type` != 'reply' and `imageid` > 0 ";
					} elseif('video' == $this->Get['type']){
						$where = " and `type` != 'reply' and `videoid` > 0 ";
					} elseif('music' == $this->Get['type']){
						$where = " and `type` != 'reply' and `musicid` > 0 ";
					}
				}
				$topic_myhome_time_limit = 0;
				if($this->Config['topic_myhome_time_limit'] > 0)
				{
					$topic_myhome_time_limit = (time() - ($this->Config['topic_myhome_time_limit'] * 86400));

					if($topic_myhome_time_limit > 0)
					{
						$where .= " and `dateline`>'$topic_myhome_time_limit' ";
					}
				}

				if($member['uid']==MEMBER_ID)
				{
					if($this->Config['ajax_topic_time'])
					{
						$this->DatabaseHandler->Query("update `".TABLE_PREFIX."members` set `lastactivity`='".time()."' where `uid`='$uid'");
					}

					$_where_add = '';
					if($topic_myhome_time_limit > 0)
					{
						$_where_add = " and `buddy_lastuptime`>'$topic_myhome_time_limit' ";
					}

					$title = '我的首页';
					$sql = "select `buddyid` from `".TABLE_PREFIX."buddys` where `uid`='{$params['uid']}' $_where_add ";
					$query = $this->DatabaseHandler->Query($sql);

					while($row = $query->GetRow())
					{
						$topic_uids[$row['buddyid']] = $row['buddyid'];
					}
				} else {
					$title = "{$member['nickname']}的微博";

					$this->_initTheme($member);
				}
				$topic_uids[$uid] = $uid;
					

			} elseif('groupview' == $params['code']){

				$gid = (int) $this->Get['gid'];

				$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&code={$this->Code}&gid={$gid}" : "");
				if($this->Get['type'])
				{
					if('pic' == $this->Get['type']){
						$where = " and `imageid` > 0 ";
					} elseif('video' == $this->Get['type']){
						$where = " and `videoid` > 0 ";
					} elseif('music' == $this->Get['type']){
						$where = " and `musicid` > 0 ";
					}

				}

				$sql = "select * from `".TABLE_PREFIX."groupfields` where `gid`='{$gid}' and uid = ".MEMBER_ID." ";
				$query = $this->DatabaseHandler->Query($sql);
				$g_view_uids = array();
				$list = array();
				while ($row = $query->GetRow())
				{
					$g_view_uids[$row['touid']] = $row['touid'];
					$groupname = $row['g_name'];
					$groupid = $row['gid'];
				}

				if($g_view_uids){

					$where_list['uid'] = "`uid` in (".implode(",",$g_view_uids).") {$where}";

				}
				else
				{
					$this->Messager("没有设置用户，无法查看这个组的微博",'index.php?mod=topic&code=group&gid='.$gid);
				}


			} elseif('tag' == $params['code']){
				$title = '我关注的话题';

				if($member['uid']!=MEMBER_ID) {
					$this->Messager("您无权查看该页面",null);
				}

				$sql = "select  TF.uid , TF.tag ,T.name , T.* from `".TABLE_PREFIX."tag` T left join `".TABLE_PREFIX."tag_favorite` TF on T.name=TF.tag where `uid`='".MEMBER_ID."'";
				$query = $this->DatabaseHandler->Query($sql);
				$tag_info = array();
				while ($row = $query->GetRow())
				{
					$tag_info[$row['id']] = $row['id'];
				}

				if(!$tag_info)
				{
					$this->Messager("没有设置关注话题,请到热门话题中选择感兴趣的话题",'index.php?mod=tag');
				}


				$sql = "select `item_id` from `".TABLE_PREFIX."topic_tag` where  `tag_id` in(".implode(",",$tag_info).")";
				$query = $this->DatabaseHandler->Query($sql);
				$topic_ids = array();
				while ($row = $query->GetRow())
				{
					$topic_ids[$row['item_id']] = $row['item_id'];
				}

				if(!$topic_ids)
				{
					$this->Messager("没有设置关注话题,请到热门话题中选择感兴趣的话题",'index.php?mod=tag');
				}

				if($this->Get['type'])
				{
					$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&code={$this->Code}&type={$this->Get['type']}" : "");
					if('pic' == $this->Get['type']){
						$where = " and `imageid` > 0 ";
					} elseif('video' == $this->Get['type']){
						$where = " and `videoid` > 0 ";
					} elseif('music' == $this->Get['type']){
						$where = " and `musicid` > 0 ";
					}
				}

				$where_list['tid'] = "`tid` in(".implode(",",$topic_ids).")";

			} elseif ('mycomment' == $params['code']) {
				$title = '评论我的';

				if($member['uid']!=MEMBER_ID) {
					$this->Messager("您无权查看该页面",null);
				}

				if ($member['comment_new']) {
					$sql = "update `".TABLE_PREFIX."members` set `comment_new`=0 where `uid`='{$member['uid']}'";
					$this->DatabaseHandler->Query($sql);

					$this->MemberHandler->MemberFields['comment_new'] = 0;
				}

				$topic_selected = 'mycomment';
				$where_list['touid'] = "`touid`='{$member['uid']}' and `type` in ('both','reply')";

			} elseif ('tocomment' == $params['code']) {
				$title = '我评论的';

				if($member['uid']!=MEMBER_ID) {
					$this->Messager("您无权查看该页面",null);
				}

				$topic_selected = 'mycomment';
				$where_list['touid'] = "`uid` = '{$member['uid']}' and `type` in ('both','reply')";

			} elseif ('myblog' == $params['code']) {
				$where = " and `type` != 'reply' ";

				if($this->Get['type'])
				{
					$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module ? "{$member['username']}&type={$this->Get['type']}" : "");

					if('forward' == $this->Get['type']){			    									$where = " and `type` in ('forward','both') ";
					} elseif('start' == $this->Get['type']){												$where = " and `type` = 'first' ";
					}
				}

				if($member['uid']!=MEMBER_ID) {
					$title = "{$member['nickname']}的微博";

					$this->_initTheme($member);
				} else {
					$title = '我的微博';
					$this->MetaKeywords ="{$member['nickname']}的微博";
				}

				$buddys = array();
				if(MEMBER_ID > 0)
				{
					$sql = "select `buddyid` as `id`,`remark` from `".TABLE_PREFIX."buddys` where `uid`='".MEMBER_ID."' and `buddyid` = ".$member['uid'];
					$query = $this->DatabaseHandler->Query($sql);
					$buddys = $query->GetRow();
				}

				$topic_selected = 'myblog';
				$topic_uids[$uid] = $uid;

			} elseif ('myfavorite' == $params['code']) {
				$topic_selected = 'myfavorite';
				$title = '我的收藏';
				if($member['uid']!=MEMBER_ID) {
					$this->Messager("您无权查看该页面",null);
				}

				$sql = "select count(*) as `total_record` from `".TABLE_PREFIX."topic_favorite` TF where TF.uid='{$uid}'";
				$query = $this->DatabaseHandler->Query($sql);
				extract($query->GetRow());

				$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>"Array"));

				$sql ="select tid,dateline as favorite_time from `".TABLE_PREFIX."topic_favorite` where uid='{$uid}' order by id desc {$page_arr['limit']}";
				$query = $this->DatabaseHandler->Query($sql);
				while ($row = $query->GetRow())
				{
					if($row['tid']<1) continue;
					$ft[$row['tid']] = my_date_format2($row['favorite_time']);
				}
				if(isset($ft)){
					$sql ="select * from ".TABLE_PREFIX."topic` where tid in(".implode(",",array_keys($ft)).")";
					$query = $this->DatabaseHandler->Query($sql);
					while ($row = $query->GetRow())
					{
						$row['favorite_time'] =$ft[$row['tid']];
						$row = $this->TopicLogic->Make($row);
						$topic_list[$row['tid']] = $row;
					}
				}


				//				$sql = "select TF.dateline as favorite_time , T.* from `".TABLE_PREFIX."topic_favorite` TF left join `".TABLE_PREFIX
				//				."topic` T on T.tid=TF.tid where TF.uid='{$uid}' order by TF.id desc {$page_arr['limit']}";
				//
				//				$query = $this->DatabaseHandler->Query($sql);
				//				while ($row = $query->GetRow())
				//				{
				//					if($row['tid']<1) continue;
				//					$row['favorite_time'] = my_date_format2($row['favorite_time']);
				//
				//					$row = $this->TopicLogic->Make($row);
				//					$topic_list[$row['tid']] = $row;
				//				}
				$topic_list_get = true;

			} elseif ('favoritemy' == $params['code']) {
				$topic_selected = 'favoritemy';
				$title = '收藏我的';
				if($member['uid']!=MEMBER_ID) {
					$this->Messager("您无权查看该页面",null);
				}

				if ($member['favoritemy_new']>0) {
					$sql = "update `".TABLE_PREFIX."members` set `favoritemy_new`=0 where `uid`='{$member['uid']}'";
					$this->DatabaseHandler->Query($sql);

					$this->MemberHandler->MemberFields['favoritemy_new'] = 0;
				}

				$sql = "select count(*) as `total_record` from `".TABLE_PREFIX."topic_favorite` TF where TF.tuid='{$uid}'";
				$query = $this->DatabaseHandler->Query($sql);
				extract($query->GetRow());

				$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>"Array"));

				$sql ="select tid,dateline as favorite_time ,uid as fuid from `".TABLE_PREFIX."topic_favorite` where tuid='{$uid}' order by id desc {$page_arr['limit']}";
				$query = $this->DatabaseHandler->Query($sql);
				while ($row = $query->GetRow())
				{
					if($row['tid']<1) continue;
					$ft[$row['tid']] = my_date_format2($row['favorite_time']);
					$ft[$row['tid']][fuid] = $row['fuid'];
					$fuids[$row['fuid']] = $row['fuid'];
				}
				if(isset($ft)){
					$sql ="select * from ".TABLE_PREFIX."topic` where tid in(".implode(",",array_keys($ft)).")";
					$query = $this->DatabaseHandler->Query($sql);
					while ($row = $query->GetRow())
					{
						
						$row['favorite_time'] =$ft[$row['tid']];
						$row = $this->TopicLogic->Make($row);
						$topic_list[$row['tid']] = $row;
						$topic_list[$row['tid']][fuid] = $ft[$row['tid']][fuid];
					}
				}

					

				//				$sql = "select TF.dateline as favorite_time , TF.uid as fuid , T.* from `".TABLE_PREFIX."topic_favorite` TF left join `"
				//				.TABLE_PREFIX."topic` T on T.tid=TF.tid where TF.tuid='{$uid}' order by TF.id desc {$page_arr['limit']}";
				//				$query = $this->DatabaseHandler->Query($sql);
				//				$fuids = array();
				//				while ($row = $query->GetRow())
				//				{
				//					if($row['tid']<1) continue;
				//
				//					$row['favorite_time'] = my_date_format2($row['favorite_time']);
				//					$row = $this->TopicLogic->Make($row);
				//					$topic_list[$row['tid']] = $row;
				//					$fuids[$row['fuid']] = $row['fuid'];
				//				}
				$favorite_members = $this->TopicLogic->GetMember($fuids,"`uid`,`ucuid`,`username`,`nickname`,`face_url`,`face`,`validate`");

				$topic_parent_disable = true;
				$topic_list_get = true;

			}

			$keyword = trim($this->Get['keyword']);
			$keyword = getSafeCode($keyword);
			if ($keyword) {
				$where_list['keyword'] = build_like_query('content,content2',$keyword);
				$query_link .= "&keyword=".urlencode($keyword);
			}

			if(!$topic_list_get)
			{
				if($topic_ids) {
					$where_list['tid'] = "`tid` in('".implode("','",$topic_ids)."') {$where}";
				}

				if(empty($keyword))
				{
					if ($topic_uids) {
						$where_list['uid'] = "`uid` in('".implode("','",$topic_uids)."')  {$where}";
					}
				}


				$where = (empty($where_list)) ? null : ' WHERE '.implode(' AND ',$where_list).' ';
				$order = $order_list ? "order by " . implode(" , ",$order_list) : "";

				$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."topic` {$where} ";
				$query = $this->DatabaseHandler->Query($sql);
				extract($query->GetRow());

				$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>'array',));

				$condition = " {$where} {$order} {$page_arr['limit']} ";

				$topic_list = $this->TopicLogic->Get($condition);
			}

			$topic_list_count = 0;
			if($topic_list) {
				$topic_list_count = count($topic_list);

				if(!$topic_parent_disable) {
					$parent_id_list = array();
					foreach ($topic_list as $row) {
						if(0 < ($p = (int) $row['parent_id'])) {
							$parent_id_list[$p] = $p;
						}
						if (0 < ($p = (int) $row['top_parent_id'])) {
							$parent_id_list[$p] = $p;
						}
					}

					if($parent_id_list) {
						$parent_list = $this->TopicLogic->Get($parent_id_list);
					}

				}
			}

			$group_list = $grouplist2 = array();
			$group_list = $this->_myGroup($member['uid']);
			if($group_list) $grouplist2 = array_slice($group_list,0,min(3,count($group_list)));

			$GroupFields = $this->_GroupFields($member['uid']);

			if($member['medal_id'])
			{
				$medal_list = $this->_Medal($member['medal_id'],$member['uid']);
			}


			$sql = "select * from `".TABLE_PREFIX."user_tag_fields` where  `uid` = '{$member['uid']}'";
			$query = $this->DatabaseHandler->Query($sql);
			$myuser_tag = array();
			while($row = $query->GetRow())
			{
				$myuser_tag[] = $row;
			}
			$list_blacklist = array();
			if(MEMBER_ID > 0)
			{
				$sql = "select `uid`,`touid` from `".TABLE_PREFIX."blacklist` where `touid` = '{$member['uid']}' and `uid` = '".MEMBER_ID."'";
				$query = $this->DatabaseHandler->Query($sql);
				$list_blacklist = $query->GetRow();
			}


			$this->Title = $title;
			   
			include($this->TemplateHandler->Template('topic_index'));
	}

	function Cms(){
		$sql = "select topicid from `" . TABLE_PREFIX . "topic_cjcms` where cjcmsid=$_GET[cjcmsid]";
		$q=$this->DatabaseHandler->Query($sql);
		$r = $q->GetRow();
		$tid = $r[topicid];
		$topic_info = $this->TopicLogic->Get($tid);
		if(isset($tid) && $topic_info){
			$this->Code=$this->ID = $tid;
			$this->View();
		}else{
			require_once ROOT_PATH.'private/classes/cjcmsclass.inc.php';
			$cjcms = new cjcmsclass($this,$this->TopicLogic);
			include ROOT_PATH . 'setting/web_net_uid.php';
			$rd = $cjcms->get_cmsurl($config['net_uid']['cmsurl'].$_GET[cjcmsid]);
			if(isset($rd[ok][tid])){
				$this->Code=$this->ID = $rd[ok][tid];
				$this->View();
			}else{
				$this->Messager($rd[err],null);
			}
		}
	}

	function View()
	{
		if ($this->ID < 1) {
			$this->Messager("请指定一个ID",null);
		}
		$per_page_num = 10;
		$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}" : "");

		$topic_info = $this->TopicLogic->Get($this->ID);
		if (!$topic_info) {
			$this->Messager("您要查看的话题已经不存在了",null);
		}


		if ($topic_info['item'] == 'qun') {
			$ret = $this->TopicLogic->_is_qun_member($topic_info['item_id'], MEMBER_ID);
			if (!$ret) {
				$this->Messager("你不是当前微群的成员，无法查看");
			}
		}

		if($topic_info['parent_id']) {
			$parent_id_list = array(
			$topic_info['parent_id'] => $topic_info['parent_id'],
			$topic_info['top_parent_id'] => $topic_info['top_parent_id'],
			);

			if($parent_id_list) {

				$parent_list = $this->TopicLogic->Get($parent_id_list);
			}

		}

		if ($topic_info['replys'] > 0) {

			$_config = array(
				'return' => 'array',
			);

			$tids = $this->TopicLogic->GetReplyIds($topic_info['tid']);


			$total_record = $topic_info['replys'];
			$page_arr = page($total_record,$per_page_num,$query_link,$_config);

			if($tids)
			{
				$condition = "where `tid` in ('".implode("','",$tids)."') order by `dateline` asc {$page_arr['limit']}";

				$reply_list = $this->TopicLogic->Get($condition);
			}
		}

		if (MEMBER_ID > 0) {
			$sql = "select * from `".TABLE_PREFIX."topic_favorite` where `uid`='".MEMBER_ID."' and `tid`='{$topic_info['tid']}'";
			$query = $this->DatabaseHandler->Query($sql);
			$is_favorite = $query->GetRow();
		}

		$member = $this->_member($topic_info['uid']);
		if($member['medal_id'])
		{
			$medal_list = $this->_Medal($member['medal_id'],$member['uid']);
		}
		$this->Title = cut_str(strip_tags($topic_info['content']),50)." - {$member['nickname']}的微博";

		$Keywords = array();
		if(strpos($topic_info['content'],'#')){
			preg_match_all('~\#([^\#]+?)\#~',$topic_info['content'],$Keywords);
		}
		if(is_array($Keywords[1]) && count($Keywords[1]))
		{
			$this->MetaKeywords = implode(',',$Keywords[1]);
		}
		$this->MetaDescription = strip_tags($topic_info['content']);

		if(MEMBER_ID != $member['uid'])
		{
			$this->_initTheme($member);
		}
		
		$_POST['tid']=$this->ID;
		require_once ROOT_PATH.'private/classes/qq.inc.php';
        $qqinc = new qqinc($this);
        $qqinc->SyncReply();

//        require_once ROOT_PATH.'private/classes/sina.inc.php';
//        $sinainc = new sinainc($this);
//        $sinainc->SyncReply();
        
		include($this->TemplateHandler->Template('topic_view'));
	}

	function Follow()
	{
		$member = $this->_member();
		if (!$member) {
			$this->Messager("请先登录",'index.php?mod=login');
		}

		$per_page_num = $this->ShowConfig['topic']['follow'];

		$_config = array(
			'return' => 'array',
		);


		if(empty($this->Get['gid']))
		{
			$page_arr = page($member['follow_count'],$per_page_num,"index.php?mod={$member['username']}&code=follow",$_config);

			$sql = "select `buddyid` as id from `".TABLE_PREFIX."buddys` where `uid`='{$member['uid']}' order by `id` desc {$page_arr['limit']}";
			$query = $this->DatabaseHandler->Query($sql);
			$uids = array();
			while ($row = $query->GetRow())
			{
				$uids[$row['id']] = $row['id'];
			}

			$member_list = array();
			if($uids)
			{
				$buddys = array();
				$_remarks = array();
				$_dateline = array();
				if(MEMBER_ID > 0)
				{
					$sql = "select `buddyid` as `id` ,`remark`,`dateline` from `".TABLE_PREFIX."buddys` where `uid`='".MEMBER_ID."' and `buddyid` in(".implode(",",$uids).") ";
					$query = $this->DatabaseHandler->Query($sql);
					while ($row = $query->GetRow())
					{
						$buddys[$row['id']] = $row['id'];

						$_remarks[$row['id']] = $row['remark'];

						$_dateline[$row['id']] = $row['dateline'];
					}
				}


				$_list = $this->TopicLogic->GetMember($uids,"`uid`,`ucuid`,`username`,`face_url`,`face`,`province`,`city`,`fans_count`,`topic_count`,`validate`,`nickname`,`gender`");
				foreach ($uids as $uid)
				{
					if(isset($_list[$uid]))
					{
						$_list[$uid]['follow_html'] = follow_html($uid,isset($buddys[$uid]));
						if($_remarks[$uid])
						{
							$_list[$uid]['remark'] = $_remarks[$uid];
						}

						$member_list[$uid] = $_list[$uid];
					}
				}

				$sql = "select `id`,`uid`,`buddyid` from `".TABLE_PREFIX."buddys` where  `uid` in (".implode(",",$uids).")";
				$query = $this->DatabaseHandler->Query($sql);
				$buddys_list = array();
				while ($row = $query->GetRow())
				{
					$buddys_list[] = $row;
				}
			}
		}
		else
		{
			$gid = (int) $this->Get['gid'];

			$sql = "select * from `".TABLE_PREFIX."group` where `id`='{$gid}' limit 0,1";
			$query = $this->DatabaseHandler->Query($sql);
			$group_view = $query->GetRow();

			$page_arr = page($group_view['group_count'],$per_page_num,"index.php?mod=topic&code=follow&gid={$gid}",$_config);


			$sql = "select * from `".TABLE_PREFIX."groupfields` where `gid`='{$gid}' order by `id` desc {$page_arr['limit']}";
			$query = $this->DatabaseHandler->Query($sql);
			$group_uids = array();
			while ($row = $query->GetRow())
			{
				$group_uids[$row['touid']] = $row['touid'];
			}

			$member_list = array();
			if($group_uids)
			{
				$g_buddys = array();
				$_remarks = array();
				if(MEMBER_ID > 0)
				{
					$sql = "select `buddyid` as `id`,`remark` from `".TABLE_PREFIX."buddys` where `uid`='".MEMBER_ID."' and `buddyid` in(".implode(",",$group_uids).")";
					$query = $this->DatabaseHandler->Query($sql);

					while ($row = $query->GetRow())
					{
						$g_buddys[$row['id']] = $row['id'];
						$_remarks[$row['id']] = $row['remark'];
					}
				}

				$g_list = $this->TopicLogic->GetMember($group_uids,"`uid`,`ucuid`,`username`,`face_url`,`face`,`province`,`city`,`fans_count`,`topic_count`,`validate`,`gender`,`nickname`,`gender`");
				foreach ($group_uids as $guid)
				{
					if(isset($g_list[$guid]))
					{
						$g_list[$guid]['follow_html'] = follow_html($guid,isset($g_buddys[$guid]));
						if($_remarks[$guid])
						{
							$g_list[$guid]['remark'] = $_remarks[$guid];
						}

						$member_list[$guid] = $g_list[$guid];
					}
				}
			}
		}

		$sql = "select  GF.touid , GF.gid, GF.g_name , GF.display ,G.group_name ,G.id , GF.* from `".TABLE_PREFIX."group` G left join `".TABLE_PREFIX."groupfields` GF on G.id=GF.gid where G.uid='".MEMBER_ID." ' ";
		$query = $this->DatabaseHandler->Query($sql);
		$user_group = array();
		while ($row = $query->GetRow())
		{
			$user_group[$row['id']] = $row;
		}

		$group_list = $grouplist2 = array();
		$group_list = $this->_myGroup($member['uid']);
		if($group_list) $grouplist2 = array_slice($group_list,0,min(4,count($group_list)));



		if($member['medal_id'])
		{
			$medal_list = $this->_Medal($member['medal_id'],$member['uid']);
		}

		$this->Title = "{$member['nickname']}关注的微博";

		include($this->TemplateHandler->Template('topic_follow'));

	}

	function ViewGroup()
	{
		$member = $this->_member();
		if (!$member) {
			$this->Messager("请先登录",'index.php?mod=login');
		}
		$gid = (int) $this->Get['gid'];

		$sql = "select * from `".TABLE_PREFIX."group` where `id`='{$gid}' limit 0,1";
		$query = $this->DatabaseHandler->Query($sql);
		$group_view = $query->GetRow();


		$_config = array(
			'return' => 'array',
		);
		$per_page_num = 30;
		$page_arr = page($member['follow_count'],$per_page_num,"index.php?mod=topic&code=group&gid={$gid}",$_config);

		$buddysList = array();
		$sql = "select `buddyid` as id from `".TABLE_PREFIX."buddys` where `uid`='{$member['uid']}' order by `id` desc {$page_arr['limit']}";
		$query = $this->DatabaseHandler->Query($sql);
		$uids = array();
		while ($row = $query->GetRow())
		{
			$uids[$row['id']] = $row['id'];
		}


		$buddys = $this->TopicLogic->GetMember($uids,"`uid`,`ucuid`,`username`,`face_url`,`face`,`province`,`city`,`fans_count`,`topic_count`,`validate`,`nickname`");
		foreach ($uids as $uid) {
			if(isset($buddys[$uid])) {
				$buddysList[$uid] = $buddys[$uid];
			}
		}


		$group_list = $grouplist2 = array();
		$group_list = $this->_myGroup($member['uid']);
		if($group_list) $grouplist2 = array_slice($group_list,0,min(4,count($group_list)));


		include($this->TemplateHandler->Template('topic_group'));
	}

	function Fans()
	{
		$member = $this->_member();
		if (!$member) {
			$this->Messager("链接错误，请检查",null);
		}
		$per_page_num = $this->ShowConfig['topic']['fans'];

		if ($member['uid']==MEMBER_ID && $member['fans_new']>0) {
			$sql = "update `".TABLE_PREFIX."members` set `fans_new`=0 where `uid`='{$member['uid']}'";
			$this->DatabaseHandler->Query($sql);

			$this->MemberHandler->MemberFields['fans_new'] = 0;
		}


		$_config = array(
			'return' => 'array',
		);
		$page_arr = page($member['fans_count'],$per_page_num,"index.php?mod={$member['username']}&code=fans",$_config);


		$sql = "select `uid` as id from `".TABLE_PREFIX."buddys` where `buddyid`='{$member['uid']}' order by `id` desc {$page_arr['limit']}";
		$query = $this->DatabaseHandler->Query($sql);
		$uids = array();
		while ($row = $query->GetRow())
		{
			$uids[$row['id']] = $row['id'];
		}


		$member_list = array();
		if($uids) {
			$buddys = array();
			if(MEMBER_ID > 0) {
				$sql = "select `buddyid` as `id` from `".TABLE_PREFIX."buddys` where `uid`='".MEMBER_ID."' and `buddyid` in(".implode(",",$uids).")";
				$query = $this->DatabaseHandler->Query($sql);
				while ($row = $query->GetRow())
				{
					$buddys[$row['id']] = $row['id'];
				}
			}

			$_list = $this->TopicLogic->GetMember($uids,"`uid`,`ucuid`,`username`,`face_url`,`face`,`province`,`city`,`fans_count`,`topic_count`,`validate`,`gender`,`face`,`nickname`");
			foreach ($uids as $uid) {
				if(isset($_list[$uid])) {

					$_list[$uid]['follow_html'] = follow_html($uid,isset($buddys[$uid]));

					$member_list[$uid] = $_list[$uid];
				}
			}
		}

		$group_list = $this->_myGroup($member['uid']);

		$this->Title = "关注{$member['nickname']}的人";
		include($this->TemplateHandler->Template('topic_fans'));
	}




	function Top()
	{

		$limit = $this->ShowConfig['topic_top']['guanzhu'];
		if($limit>0 && false === ($r_users = cache("misc/top_users-{$limit}",$this->CacheConfig['topic_top']['guanzhu']))) {
			$r_users = $this->TopicLogic->GetMember("where face!='' order by `fans_count` desc limit {$limit}","`uid`,`ucuid`,`username`,`fans_count`,`validate`,`province`,`city`,`face`,`nickname`");

			cache($r_users);
		}

		$limit = $this->ShowConfig['topic_top']['renqi'];

		if ($limit>0 && false == ($day7_r_buddys = cache("misc/top_day7_r_buddys",$this->CacheConfig['topic_top']['renqi']))) {
			$day7_r_buddys = array();

			$sql = "select DISTINCT(B.buddyid) AS buddyid , COUNT(B.uid) AS count  from `".TABLE_PREFIX."buddys` B left join `".TABLE_PREFIX."members` M on B.buddyid=M.uid WHERE B.dateline>='".(time() - 86400 * 7)."' and M.face!='' GROUP BY buddyid ORDER BY count DESC LIMIT {$limit}";

			$query = $this->DatabaseHandler->Query($sql);
			$uids = $_ids = array();
			while ($row = $query->GetRow())
			{
				$uids[$row['buddyid']] = $row['buddyid'];
				$_ids[$row['buddyid']] = $row;
			}


			if($_ids) {
				$_list = $this->TopicLogic->GetMember($uids,"`uid`,`ucuid`,`username`,`fans_count`,`validate`,`province`,`city`,`face`,`nickname`");

				foreach ($_ids as $id=>$row) {
					$row['uid'] = $_list[$id]['uid'];
					$row['username'] = $_list[$id]['username'];
					$row['nickname'] = $_list[$id]['nickname'];
					$row['face'] = $_list[$id]['face'];
					$row['from_area'] = $_list[$id]['from_area'];
					$row['validate_html'] = $_list[$id]['validate_html'];
					if(isset($_list[$id])) {
						$day7_r_buddys[$id] = $row;
					}
				}
			}
			cache($day7_r_buddys);
		}

		$limit = $this->ShowConfig['topic_top']['huoyue'];
		if ($limit>0 && false == ($day7_r_users = cache("misc/top_day7_r_users",$this->CacheConfig['topic_top']['huoyue']))) {

			$sql = "select DISTINCT(username) AS username , uid , COUNT(tid) AS count from ".TABLE_PREFIX.
			"topic WHERE dateline>='".(time() - 86400 * 7)."' GROUP BY username ORDER BY count DESC LIMIT {$limit}";
			$query = $this->DatabaseHandler->Query($sql);
			$uids = $day7_r_users = array();
			while ($row = $query->GetRow())
			{
				$day7_r_users[$row['username']] = $row;
				$uids[$row['username']] = $row['username'];
				$buids[$row['uid']] = $row['uid'];
			}

			//			$sql = "select DISTINCT(T.username) AS username , T.uid AS uid , COUNT(T.tid) AS count from `".TABLE_PREFIX."topic` T left join `"
			//			.TABLE_PREFIX."members` M on T.uid=M.uid WHERE T.dateline>='".(time() - 86400 * 7)
			//			."' and M.face!='' GROUP BY username ORDER BY count DESC LIMIT {$limit}";
			//
			//			$query = $this->DatabaseHandler->Query($sql);
			//			$uids = $day7_r_users = array();
			//			while ($row = $query->GetRow())
			//			{
			//				$day7_r_users[$row['username']] = $row;
			//				$uids[$row['username']] = $row['username'];
			//				$buids[$row['uid']] = $row['uid'];
			//			}

			if ($day7_r_users) {
				$members = $this->TopicLogic->GetMember("where `username` in ('".implode("','",$uids)."') limit {$limit}","`uid`,`ucuid`,`username`,`fans_count`,`validate`,`province`,`city`,`face`,`nickname`");

				foreach ($members as $_m) {
					$day7_r_users[$_m['username']]['validate_html'] = $_m['validate_html'];
					$day7_r_users[$_m['username']]['face'] = $_m['face'];
					$day7_r_users[$_m['username']]['uid'] = $_m['uid'];
					$day7_r_users[$_m['username']]['from_area'] = $_m['from_area'];
					$day7_r_users[$_m['username']]['nickname'] = $_m['nickname'];
				}
			}
			cache($day7_r_users);
		}

		$limit = $this->ShowConfig['topic_top']['yingxiang'];
		if ($limit>0 && false == ($day7_r_topics = cache("misc/top_day7_r_topics",$this->CacheConfig['topic_top']['yingxiang']))) {
			$sql = "select DISTINCT(touid) AS touid , COUNT(tid) AS count from `"
			.TABLE_PREFIX."topic` WHERE  dateline>='".(time() - 86400 * 7)."' and touid > 0  GROUP BY username ORDER BY count DESC  LIMIT {$limit}";
			
			$query = $this->DatabaseHandler->Query($sql);
			$uids = $day7_r_topics = array();
			while ($row = $query->GetRow())
			{
				$uids[$row['touid']] = $row['touid'];

			}
			if ($uids) {
				$members = $this->TopicLogic->GetMember("where `uid` in ('".implode("','",$uids)."') limit {$limit}","`uid`,`ucuid`,`username`,`fans_count`,`validate`,`province`,`city`,`face`,`nickname`");

				foreach ($members as $_m) {
					$day7_r_topics[$_m['username']]['validate_html'] = $_m['validate_html'];
					$day7_r_topics[$_m['username']]['face'] = $_m['face'];
					$day7_r_topics[$_m['username']]['uid'] = $_m['uid'];
					$day7_r_topics[$_m['username']]['from_area'] = $_m['from_area'];
					$day7_r_topics[$_m['username']]['nickname'] = $_m['nickname'];
				}

			}

			cache($day7_r_topics);
		}

		$this->Title = "人气用户推荐";
		include($this->TemplateHandler->Template('topic_top'));
	}

	function Hot()
	{
		if(MEMBER_ID > 0) {
			$member = $this->_member(MEMBER_ID);
		}

		if('forward' == $this->Code)
		{
			$title = '热门转发';

			$d_list = array(1=>'近一天',7=>'近一周',14=>'近两周',30=>'近一月',);
			$d = isset($d_list[$this->Get['d']]) ? $this->Get['d'] : 7;
			$time = $d * 86400;
			$dateline = time() - $time;

			$per_page_num = $this->ShowConfig['topic_hot']["day{$d}"];
			$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}&d={$d}" : "");

			$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."topic` Where `forwards` > 0 and  dateline >= $dateline";
			$query = $this->DatabaseHandler->Query($sql);
			extract($query->GetRow());

			$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));

			$limit = $this->ShowConfig['topic_hot']["day{$d}"];

			$condition = " where `forwards` > 0 and  dateline >= $dateline order by `forwards` DESC , `dateline` DESC {$page_arr['limit']}";

			$cache_time = ($this->CacheConfig['topic_hot']["day{$d}"] ? $this->CacheConfig['topic_hot']["day{$d}"] : $time / 90);


			$topics = $this->TopicLogic->Get($condition);

		} elseif('reply' == $this->Code) {

			$title = '热门评论';

			$d_list = array(1=>'近一天',7=>'近一周',14=>'近两周',30=>'近一月',);
			$d = isset($d_list[$this->Get['d']]) ? $this->Get['d'] : 7;
			$time = $d * 86400;
			$dateline = time() - $time;


			$per_page_num = $this->ShowConfig['reply_hot']["day{$d}"];
			$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}&d={$d}" : "");


			$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."topic` Where `replys` > 0 and dateline >= $dateline " ;
			$query = $this->DatabaseHandler->Query($sql);
			extract($query->GetRow());

			$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));
			$condition = " where `replys` > 0 and  dateline >= $dateline order by `replys` DESC , `dateline` DESC {$page_arr['limit']}";


			$topics = $this->TopicLogic->Get($condition);

		} elseif ('new' == $this->Code) {

			$title = '最新微博';

			$per_page_num = $this->ShowConfig['topic_new']['topic'];
			$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}" : "");

			$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."topic` where `type` != 'reply'";
			$query = $this->DatabaseHandler->Query($sql);
			extract($query->GetRow());

			$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));
			$condition = " where `type` != 'reply' order by `dateline` desc {$page_arr['limit']} ";

			if($this->ShowConfig['topic_new']['topic'] > 0) {
				if(false === ($topics = cache("misc/new_topics","{$page_arr['limit']}"))) {
					$topics = $this->TopicLogic->Get($condition);
					cache($topics);
				}

			}

		}
		elseif('newreply' == $this->Code)
		{
			$title = '最新评论';

			$per_page_num = $this->ShowConfig['new_reply']['reply'];
			$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}" : "");

			$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."topic` Where `replys` > 0  and `type` = 'first'";
			$query = $this->DatabaseHandler->Query($sql);
			extract($query->GetRow());

			$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));

			$condition = " where  `type` = 'reply'  order by `dateline` DESC {$page_arr['limit']}";

			$topics = $this->TopicLogic->Get($condition);

		}
		$parent_id_list = array();
		if ($topics) {
			foreach ($topics as $row) {
				if(0 < ($p = (int) $row['parent_id'])) {
					$parent_id_list[$p] = $p;
				}
				if (0 < ($p = (int) $row['top_parent_id'])) {
					$parent_id_list[$p] = $p;
				}
			}
		}
		if($parent_id_list) {
			$parent_list = $this->TopicLogic->Get($parent_id_list);
		}


		$Gz_limit = $this->ShowConfig['topic_index']['guanzhu'];
		$concern_users = $this->TopicLogic->GetMember("order by `fans_count` desc limit {$Gz_limit}","`uid`,`ucuid`,`username`,`face_url`,`face`,`fans_count`,`validate`,`nickname`");


		$Tg_limit = $this->ShowConfig['topic_new']['tag'];
		$Tg_date  = $this->CacheConfig['topic_new']['day_tag'];
			
		$dateline = time() - $Tg_date;
		$sql = "select `id`,`name`,`topic_count`,`dateline` from `".TABLE_PREFIX."tag` where `dateline` > '{$dateline}' order by `topic_count` desc limit {$Tg_limit}";
		$query = $this->DatabaseHandler->Query($sql);
		$tags = $query->GetAll();
			

		$this->Title = $title;

		include($this->TemplateHandler->Template('topic_new'));

	}

	function _member($uid=0)
	{
		$member = array();
		if($uid < 1)
		{
			$mod_original = ($this->Post['mod_original'] ? $this->Post['mod_original'] : $this->Get['mod_original']);
			if($mod_original)
			{
				$mod_original = getSafeCode($mod_original);

				$condition = "where `username`='{$mod_original}' limit 1";

				$members = $this->TopicLogic->GetMember($condition);

				if(is_array($members))
				{
					reset($members);
					$member = current($members);
				}
			}
		}
		$uid = (int) ($uid ? $uid : MEMBER_ID);
		if($uid > 0 && !$member)
		{
			$member = $this->TopicLogic->GetMember($uid);
		}
		if(!$member)
		{
			return false;
		}
		$uid = $member['uid'];

		if (!$member['follow_html'] && $uid!=MEMBER_ID && MEMBER_ID>0)
		{
			$sql = "select * from `".TABLE_PREFIX."buddys` where `uid`='".MEMBER_ID."' and `buddyid`='{$uid}'";
			$query = $this->DatabaseHandler->Query($sql);
			$member['follow_html'] = follow_html($member['uid'],$query->GetNumRows()>0);
		}

		if(true === UCENTER_FACE && MEMBER_ID == $uid && MEMBER_UCUID > 0 && !($member['__face__']))
		{
			include_once(ROOT_PATH . 'uc_client/client.php');

			$uc_check_result = uc_check_avatar(MEMBER_UCUID);

			if($uc_check_result)
			{
				$this->DatabaseHandler->Query("update ".TABLE_PREFIX."members set `face`='./images/no.gif' where `uid`='{$uid}'");
			}
		}

		return $member;
	}

	function _followList($uid,$num=6) {
		$uid = max(0,(int) $uid);

		$member_list = array();
		if($uid > 0)
		{
			$sql = "select `buddyid` from `".TABLE_PREFIX."buddys` where `uid`='{$uid}' order by `id` desc limit {$num}";
			$query = $this->DatabaseHandler->Query($sql);

			$ids = array();
			while ($row = $query->GetRow())
			{
				$id = $row['buddyid'];

				$ids[$id] = $id;
			}

			if($ids)
			{
				$_list = $this->TopicLogic->GetMember($ids);

				foreach ($ids as $id) {
					if($id > 0 && isset($_list[$id])) {
						$member_list[$id] = $_list[$id];
					}
				}
			}
		}


		return $member_list;
	}

	function _fansList($uid,$num=6) {
		$uid = max(0,(int) $uid);

		$member_list = array();
		if($uid > 0)
		{
			$sql = "select `uid` from `".TABLE_PREFIX."buddys` where `buddyid`='{$uid}' order by `id` desc limit {$num}";
			$query = $this->DatabaseHandler->Query($sql);
			$ids = array();
			while ($row = $query->GetRow())
			{
				$id = $row['uid'];

				$ids[$id] = $id;
			}

			if($ids)
			{
				$_list = $this->TopicLogic->GetMember($ids);

				foreach ($ids as $id) {
					if($id > 0 && isset($_list[$id])) {
						$member_list[$id] = $_list[$id];
					}
				}
			}
		}

		return $member_list;
	}

	function _recommendTag($day=1,$limit=12,$cache_time=0)
	{
		if($limit < 1) return false;

		$time = $day * 86400;
		$cache_time = ($cache_time ? $cache_time : $time / 90);

		if (false === ($list = cache("misc/recommendTopicTag-{$day}-{$limit}",$cache_time))) {
			$dateline = time() - $time;
			$sql = "SELECT DISTINCT(tag_id) AS tag_id, COUNT(item_id) AS item_id_count FROM `".TABLE_PREFIX."topic_tag` WHERE dateline>=$dateline GROUP BY tag_id ORDER BY item_id_count DESC LIMIT {$limit}";
			$query = $this->DatabaseHandler->Query($sql);
			$ids = array();
			while ($row = $query->GetRow())
			{
				$ids[$row['tag_id']] = $row['tag_id'];
			}
			$list = array();
			if($ids) {
				$sql = "select `id`,`name`,`topic_count` from `".TABLE_PREFIX."tag` where `id` in('".implode("','",$ids)."')";
				$query = $this->DatabaseHandler->Query($sql);
				$list = $query->GetAll();
			}

			cache($list);
		}

		return $list;

	}

	function _recommendUser($day=1,$limit=12,$cache_time=0)
	{
		if($limit < 1) return false;

		$time = $day * 86400;
		$cache_time = ($cache_time ? $cache_time : $time / 90);

		if (false === ($list = cache("misc/recommendTopicUser-{$day}-{$limit}",$cache_time))) {
			$dateline = time() - $time;
			$sql = "select DISTINCT(uid) AS uid , COUNT(tid) AS topics from `".TABLE_PREFIX."topic` WHERE dateline>=$dateline GROUP BY uid ORDER BY topics DESC LIMIT {$limit}";
			$query = $this->DatabaseHandler->Query($sql);
			$uids = array();
			while ($row = $query->GetRow())
			{
				$uids[$row['uid']] = $row['uid'];
			}
			$list = array();
			if($uids) {
				$_list = $this->TopicLogic->GetMember($uids,"`uid`,`ucuid`,`username`,`face_url`,`face`,`aboutme`,`validate`,`nickname`");
				foreach ($uids as $uid) {
					if ($uid > 0 && isset($_list[$uid])) {
						$list[$uid] = $_list[$uid];
					}
				}
			}
			cache($list);
		}

		return $list;
	}

	function _recommendTopic($day = 1,$limit=12,$cache_time=0)
	{
		if($limit < 1) return false;

		$time = $day * 86400;
		$cache_time = ($cache_time ? $cache_time : $time / 90);

		if (false === ($list = cache("misc/recommendTopic-{$day}-{$limit}",$cache_time))) {
			$dateline = time() - $time;
			$sql = "SELECT DISTINCT(roottid) AS item_id, COUNT(tid) AS `count` FROM `".TABLE_PREFIX."topic` WHERE dateline>=$dateline GROUP BY roottid ORDER BY `count` DESC LIMIT {$limit}";
			$query = $this->DatabaseHandler->Query($sql);
			$list = $ids = array();
			while ($row = $query->GetRow())
			{
				if($row['item_id'] > 0) {
					$ids[$row['item_id']] = $row['item_id'];
				}
			}
			if($ids) {
				$list = $this->TopicLogic->Get(" where `tid` in ('".implode("','",$ids)."') order by `dateline` desc limit {$limit}");
			}

			cache($list);
		}

		return $list;
	}

	function _recommendBuddy($day = 20,$limit=12)
	{
		$time = $day * 86400;

		if (false === ($list = cache("misc/recommendBuddy-{$day}-{$limit}",$time / 90))) {
			$dateline = time() - $time;
			$sql = "SELECT DISTINCT(uid) AS item_id, COUNT(buddyid) AS `count` FROM `".TABLE_PREFIX."buddys` WHERE dateline>=$dateline GROUP BY uid ORDER BY `count` DESC LIMIT {$limit}";
			$query = $this->DatabaseHandler->Query($sql);
			$ids = array();
			while ($row = $query->GetRow())
			{
				$ids[$row['item_id']] = $row['item_id'];
			}
			if($ids) $_list = $this->TopicLogic->GetMember($ids);

			$list = array();
			if($_list) {
				foreach ($_list as $row) {
					$rs = array();
					$rs['username'] = $row['username'];
					$rs['face'] = $row['face'];
					$rs['fans_count'] = $row['fans_count'];

					$list[$row['uid']] = $rs;
				}
			}

			cache($list);
		}

		return $list;
	}


	function _myFavoriteTags($limit=12)
	{
		$uid = MEMBER_ID;

		$sql = "select * from `".TABLE_PREFIX."tag_favorite` where `uid`='{$uid}' order by `id` desc limit {$limit} ";
		$query = $this->DatabaseHandler->Query($sql);
		$list = $query->GetAll();

		return $list;
	}

	function _guestIndex()
	{
		if(MEMBER_ID > 0) {
			$member = $this->_member(MEMBER_ID);
		}else{
			$nurl = base64_encode(base64_encode("http://t.caijing.com.cn"));
			header('location:http://service.caijing.com.cn/auth/register/url/'.$nurl);
			exit;
		}

		$time_config = ConfigHandler::get('time');


		$limit = $this->ShowConfig['topic_index']['guanzhu'];
		if($limit > 0) {
			if(false === ($r_users = cache("index/r_users",$this->CacheConfig['topic_index']['guanzhu']))) {
				$r_users = $this->TopicLogic->GetMember("where face !='' order by `fans_count` desc limit {$limit}","`uid`,`ucuid`,`username`,`face_url`,`face`,`fans_count`,`validate`,`nickname`");

				cache($r_users);
			}
		}

		$day2_r_users = $this->_recommendUser(7,$this->ShowConfig['topic_index']['new_user'],$this->CacheConfig['topic_index']['new_user']);


		$r_tags = $this->_recommendTag(2,$this->ShowConfig['topic_index']['hot_tag'],$this->CacheConfig['topic_index']['hot_tag']);


		if ($this->ShowConfig['topic_index']['recommend_topic']) {
			$condition = '';
			if($time_config['topic_index']['recommend_topic']) {
				$rtus = explode(',',$time_config['topic_index']['recommend_topic']);
				foreach ($rtus as $k=>$v) {
					$rtus[$k] = trim($v);
				}
				$sql = "select `uid` from `".TABLE_PREFIX."members` where `username` in ('".implode("','",$rtus)."')";
				$query = $this->DatabaseHandler->Query($sql);
				$rus = array();
				while ($row = $query->GetRow())
				{
					$rus[$row['uid']] = $row['uid'];
				}

				$condition = " `uid` in ('".implode("','",$rus)."') ";
			}
			$condition .= " where `type` != 'reply' order by `dateline` desc limit {$this->ShowConfig['topic_index']['recommend_topic']}";


			if (false === ($recommend_topics = cache("index/recommend_topics",$this->CacheConfig['topic_index']['recommend_topic']))) {
				$recommend_topics = $this->TopicLogic->Get($condition);

				cache($recommend_topics);
			}
		}

		$parent_id_list = array();
		if ($r_topics) {
			foreach ($r_topics as $row) {
				if(0 < ($p = (int) $row['parent_id'])) {
					$parent_id_list[$p] = $p;
				}
				if (0 < ($p = (int) $row['top_parent_id'])) {
					$parent_id_list[$p] = $p;
				}
			}
		}
		if($recommend_topics) {
			foreach ($recommend_topics as $row) {
				if(0 < ($p = (int) $row['parent_id'])) {
					$parent_id_list[$p] = $p;
				}
				if (0 < ($p = (int) $row['top_parent_id'])) {
					$parent_id_list[$p] = $p;
				}
			}
		}
		if($parent_id_list) {
			$parent_list = $this->TopicLogic->Get($parent_id_list);
		}


		if(false===($list_notice = cache('misc/_guestIndex-list_notice',86400)))
		{
			$sql="select `id`,`title` from ".TABLE_PREFIX.'notice'." order by `id` desc limit 5 ";
			$query = $this->DatabaseHandler->Query($sql);
			$list_notice = array();
			while ($row = $query->GetRow())
			{
				$row['titles']	= $row['title'];
				$row['title'] 	= cutstr($row['title'],30);
				$list_notice[] 	= $row;
			}

			cache($list_notice);
		}


		$this->MetaKeywords = $this->Config['index_meta_keywords'];
		$this->MetaDescription = $this->Config['index_meta_description'];

		include($this->TemplateHandler->Template('topic_index_guest'));
	}

	function _myGroup($uid=0,$limit='')
	{

		$order = 'order by `group_count` desc';

		$sql="Select `id`,`group_name`,`group_count` From ".TABLE_PREFIX.'group'." where `uid` = '{$uid}' {$order} {$limit}";
		$query = $this->DatabaseHandler->Query($sql);
		$list = $query->GetAll();

		return $list;
	}

	function _GroupFields($uid=0)
	{
		$list = array();

		if(MEMBER_ID > 0)
		{
			$sql="Select * From ".TABLE_PREFIX.'groupfields'." where `touid` = '{$uid}' and `uid` = '".MEMBER_ID."' order by `id` desc";

			$query = $this->DatabaseHandler->Query($sql);
			$list = $query->GetAll();
		}

		return $list;
	}

	function _myRemark()
	{
		$list = array();

		if(MEMBER_ID > 0)
		{
			$sql="SELECT * FROM ".TABLE_PREFIX.'buddys'." where `uid` =".MEMBER_ID."";
			$query = $this->DatabaseHandler->Query($sql);
			$list = $query->GetAll();
		}

		return $list;
	}

	function _Medal($medalid=0,$uid=0)
	{
			
		$unids = array(3=>1,4=>1,5=>1,);
		$_ids = (is_numeric($medalid) ? (array) $medalid : explode(',',$medalid));
		$ids = array();
		foreach($_ids as $_id)
		{
			$_id = is_numeric($_id) ? $_id : 0;
			if($_id > 0 && !isset($unids[$_id]))
			{
				$ids[$_id] = $_id;
			}
		}

		$medal_list = array();
		if($ids)
		{
			$sql = "select  U_MEDAL.dateline ,  MEDAL.medal_img , MEDAL.conditions , MEDAL.medal_name ,MEDAL.medal_depict ,MEDAL.id , U_MEDAL.* from `".TABLE_PREFIX."medal` MEDAL left join `".TABLE_PREFIX."user_medal` U_MEDAL on MEDAL.id=U_MEDAL.medalid where U_MEDAL.uid='{$uid}' ";

			$query = $this->DatabaseHandler->Query($sql);
			while ($row = $query->GetRow())
			{
				$row['dateline'] = date('m-d日 H:s ',$row['dateline']);
				$medal_list[$row['id']] = $row;
			}
		}
			
		return $medal_list;
	}



}

?>
